package com.yungu.swift.order.processor;

import com.yungu.swift.autoconfig.redis.RedisCacheService;
import com.yungu.swift.order.consts.Constants;
import com.yungu.swift.order.model.vo.PayVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Zerrion
 * @date 2018/5/8
 * 缓存处理器
 */
@Component
public class CacheProcessor implements PayProcessor {

    private static final Logger logger = LoggerFactory.getLogger(CacheProcessor.class);
    @Autowired
    private RedisCacheService redisCacheService;

    @Override
    public void process(PayVo payVo) {
        //回调锁先清除
        if (payVo.getCallback()) {
            removeLock(payVo.getDescription(), Constants.CALLBACK_CACHE_KEY + payVo.getOrderUuid(), "回调缓存锁");
        }
        //入口缓存锁
        removeLock(payVo.getDescription(), Constants.ORDER_CACHE_KEY + payVo.getOrderUuid(), "入口缓存锁");
        logger.info(payVo.getDescription() + "--处理缓存锁逻辑完毕");
    }

    private void removeLock(String desc, String cacheKey, String lockName) {
        logger.info(desc + "--尝试清除" + lockName);
        if (redisCacheService.hasKey(cacheKey)) {
            redisCacheService.delete(cacheKey);
            logger.info(desc + "--清除" + lockName);
        } else {
            logger.info(desc + "--" + lockName + "不存在");
        }
    }

}
